﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace monitor_server_2_WPF//.ControlTabs.CustomControls
{
    /// <summary>
    /// Логика взаимодействия для NumericUpDown.xaml
    /// </summary>
    public partial class NumericUpDown
    {
        private int _minValue;
        private int _maxValue = 999;
        private string _curValue = "0";

        public event TextChangedEventHandler DataChangedEvent;

        protected virtual void OnDataChangedEvent(object sender, TextChangedEventArgs e)
        {
            var handler = DataChangedEvent;
            if (handler != null) handler(sender, e);
        }

        public NumericUpDown()
        {
            InitializeComponent();
            ContentBox.TextChanged += OnDataChangedEvent;
        }

        public NumericUpDown(int minValue, int maxValue)
        {
            _minValue = minValue;
            _maxValue = maxValue;
        }

        public int MinimumValue
        {
            get { return _minValue; }
            set
            {
                if(_minValue > _maxValue)
                    throw new Exception("Minimum value can't be more than maximum");
                _minValue = value;
                if(int.Parse(_curValue) < _minValue)
                    ContentBox.Text = _minValue.ToString(CultureInfo.InvariantCulture);
            }
        }

        public int MaximumValue
        {
            get { return _maxValue; }
            set
            {
                if (_minValue > _maxValue)
                    throw new Exception("Maximum value can't be less than minimum");
                _maxValue = value;
                if (int.Parse(_curValue) > _maxValue)
                    ContentBox.Text = _maxValue.ToString(CultureInfo.InvariantCulture);
            }
        }

        public int Value
        {
            get { return int.Parse(_curValue); }
            set
            {
                if (value > _maxValue || value < _minValue)
                {
                    if (value != 0)
                        throw new Exception("Value is out of bounds");
                }
                _curValue = value.ToString(CultureInfo.InvariantCulture);
                ContentBox.Text = _curValue;
            }
        }

        //Value validation
        private void ContentBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var tb = sender as TextBox;
            int a;
            if (!int.TryParse(tb.Text, out a))
            {
                tb.Text = _curValue;
            }
            else
            {
                if (!(a < _minValue || a > _maxValue))
                    _curValue = tb.Text;
                else
                {
                    if (a < _minValue)
                        tb.Text = _curValue;// = _minValue.ToString(CultureInfo.InvariantCulture);
                    else if (a > _maxValue)
                        tb.Text = _curValue;// = _maxValue.ToString(CultureInfo.InvariantCulture);
                }
            }
        }

        //Increase value
        private void UpButton_Click(object sender, RoutedEventArgs e)
        {
            int a = int.Parse(ContentBox.Text);
            if (a < _maxValue)
                ContentBox.Text = (a + 1).ToString(CultureInfo.InvariantCulture);
        }

        //Decrease value
        private void DownButton_Click(object sender, RoutedEventArgs e)
        {
            int a = int.Parse(ContentBox.Text);
            if (a > _minValue)
                ContentBox.Text = (a - 1).ToString(CultureInfo.InvariantCulture);
        }
    }
}
